package com.latefly.skewwork.ui.page.tools.todo

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.latefly.skewwork.data.dto.vo.TodoItem
import com.latefly.skewwork.data.dto.vo.generateRandomTodoItem
import com.latefly.skewwork.data.dto.vo.randomTin

@Composable
fun TodoScreen(
    items: List<TodoItem>,
    onAddItem: (TodoItem) -> Unit,
    onRemoveItem: (TodoItem) -> Unit,
    currentlyEditing: TodoItem?,
    onStartEdit: (TodoItem) -> Unit,
    onEditItemChange: (TodoItem) -> Unit,
    onEditDone: (TodoItem) -> Unit,
) {
    Column {

        val enableTopSection = (currentlyEditing == null)
        TodoItemInputBackground(elevate = true) {
            if (enableTopSection) {
                TodoItemEntryInput(onItemComplete = onAddItem)
            } else {
                Text(
                    text = "Edit item!",
                    style = MaterialTheme.typography.titleLarge,
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .align(Alignment.CenterVertically)
                        .padding(16.dp)
                        .fillMaxWidth()

                )
            }
        }

        LazyColumn(
            modifier = Modifier.weight(1f),
            contentPadding = PaddingValues(top = 8.dp),
        ) {
            items(items) { todo ->
                if (todo.id == currentlyEditing?.id) {
                    TodoItemInlineEditor(
                        item = currentlyEditing,
                        onEditItemChange = onEditItemChange,
                        onEditDone = { onEditDone(todo) },
                        onRemoveItem = { onRemoveItem(todo) }
                    )
                } else {
                    TodoRow(
                        todo,
                        modifier = Modifier.fillMaxWidth(),
                        onItemClicked = onStartEdit
                    )
                }


            }
        }
        Button(
            modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth(),
            onClick = { onAddItem(generateRandomTodoItem()) }
        ) {
            Text(text = "Add item random")
        }
    }
}

@Composable
fun TodoRow(
    item: TodoItem,
    modifier: Modifier = Modifier,
    onItemClicked: (TodoItem) -> Unit
) {
    Row(
        modifier = Modifier
            .clickable { onItemClicked(item) }
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween //水平均匀分布
    ) {

        Text(text = item.task)

        //状态记忆icon的透明度
        val alpha: Float = remember(item.id) {
            randomTin()
        }
        Icon(
            imageVector = item.icon.imageVector,
            tint = LocalContentColor.current.copy(alpha = alpha),
            contentDescription = stringResource(id = item.icon.contentDescription)
        )
    }
}